import * as modules from './thread.worker.table'

self.onProgress = function (progress) {
    self.postMessage({
        id: this.id,
        event: 'onProgress',
        data: progress
    })
}
self.onmessage = async function (event) {
    if (event.data == 'close') {
        return self.close()
    }
    const { id, command, data, module } = event.data
    const result = {
        id,
        command
    }
    try {
        const fn = modules?.[module]?.[command]
        if (!fn) {
            throw new Error(command + '不存在')
        }
        const context = Object.assign(
            {
                id
            },
            self
        )
        result.data = await fn.call(context, data)
    } catch (error) {
        result.data = error
    } finally {
        self.postMessage(result)
    }
}
